package com.kashow.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.kashow.AppContext;
import com.kashow.R;
import com.kashow.base.adapter.QuickAdapter;
import com.kashow.injector.component.DaggerFragmentComponent;
import com.kashow.injector.component.FragmentComponent;
import com.kashow.injector.module.FragmentModule;
import com.kashow.ui.view.EmptyLayout;

import java.util.List;

import butterknife.BindView;

public abstract class CommonListFragment<T> extends BaseSupportFragment{
    protected FragmentComponent mFragmentComponent;

    protected static final String ARG_PARAM1 = "param1";
    protected static final String ARG_PARAM2 = "param2";
    protected static final String ARG_DATA = "data";

    public SwipeRefreshLayout mSwipeRefresh;
    @BindView(R.id.base_recycler_view)
    public RecyclerView mRecyclerView;
    protected EmptyLayout mEmptyLayout;
    protected List<T> mData;
    protected QuickAdapter<T> mAdapter;
    protected int mAdapterResId = 0;
    protected int mHeaderResId = 0;
    protected View mHeaderView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mData = (List<T>) getArguments().getSerializable(ARG_DATA);
            mAdapterResId = getArguments().getInt(ARG_PARAM1,0);
            mHeaderResId = getArguments().getInt(ARG_PARAM2,0);
        }
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mFragmentComponent = DaggerFragmentComponent.builder()
                .fragmentModule(new FragmentModule(this))
                .applicationComponent(((AppContext)getActivity().getApplication()).component())
                .build();

        initInject();
        if (hasRefresh()) {
            initRefreshView();
        }
        initRecyclerView();
        initEventAndData();
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_common_list;
    }

    public abstract void initInject();

    public void initRecyclerView() {
        mRecyclerView.setLayoutManager(getLayoutManager());
        mRecyclerView.setAdapter(mAdapter = getAdapter());
        if (getItemDecoration() != null) {
            mRecyclerView.addItemDecoration(getItemDecoration());
        }
        mAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mAdapter.isFirstOnly(false);
        initHeaderView();
        initEmptyView();
    }

    public void initRefreshView() {
        mSwipeRefresh = (SwipeRefreshLayout) mRootView.findViewById(R.id.base_swipe_refresh);
        mSwipeRefresh.setEnabled(isRefreshEnable());
    }

    public boolean isRefreshEnable() {
        return false;
    }

    public boolean hasRefresh() {
        return true;
    }

    public void initEmptyView() {
        mEmptyLayout = new EmptyLayout(getContext());
        mEmptyLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        mAdapter.setEmptyView(mEmptyLayout);
        mEmptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
    }

    public void initHeaderView() {
        if (mHeaderResId!= 0) {
            mHeaderView = LayoutInflater.from(getContext()).inflate(mHeaderResId,null);
            mHeaderView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,dip2px(40.0f)));
            mAdapter.addHeaderView(mHeaderView);
        }
    }

    public  QuickAdapter<T> getAdapter(){
        return new QuickAdapter<T>(mAdapterResId,mData) {
            @Override
            protected void convert(BaseViewHolder baseViewHolder, T t) {
                CommonListFragment.this.convert(baseViewHolder,t);
            }
        };
    }


    public RecyclerView.ItemDecoration getItemDecoration() {
        return null;
    }


    public RecyclerView.LayoutManager getLayoutManager() {
        return new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
    }

    protected abstract void convert(BaseViewHolder baseViewHolder, T item);

    @Override
    protected boolean hasToolbar() {
        return false;
    }

    public void addData(List<T> newData) {
        mAdapter.addData(newData);
    }

    public void setData(List<T> data) {
        mAdapter.setNewData(data);
    }

}
